import pygame

pygame.init()

# 设置窗口大小
width, height = 800, 600
display = pygame.display

display.set_caption('游戏demo')
display.set_mode((width, height))
screen = display.get_surface()

# 加载背景图片 并缩放到宽高
bgImg = pygame.image.load('./assets/images/bg.jpeg')
bgImg = pygame.transform.scale(bgImg, (width, height))

# 加载英雄图片
heroImg = pygame.image.load('./assets/images/ml.png')

# 创建人物
hero = pygame.sprite.Sprite()
hero.image = heroImg
hero.rect = hero.image.get_rect()
hero.rect.x = width/30
hero.rect.y = height/2

# 玩家组
playerGroup = pygame.sprite.Group()
playerGroup.add(hero)

# 游戏主循环
while True:

    # 处理事件
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            keys = pygame.key.get_pressed()
            if keys[pygame.K_DOWN]:
                hero.rect.y += 10
            if keys[pygame.K_UP]:
                hero.rect.y -= 10
            if keys[pygame.K_LEFT]:
                hero.rect.x -= 10
            if keys[pygame.K_RIGHT]:
                hero.rect.x += 10

    # 画图部分
    screen.blit(bgImg, (0, 0))
    playerGroup.draw(screen)
    pygame.display.update()




